
package com.deepcode.StringProcess.strategy.impl;


import com.deepcode.StringProcess.domain.CharObject;
import com.deepcode.StringProcess.strategy.CharacterReplacementStrategy;

/**
 * Previous Alphabet Character Strategy：replace them with a
 * single character that comes before it alphabetically.
 */
public class PreviousAlphabetCharacterStrategy implements CharacterReplacementStrategy {

    @Override
    public char replace(CharObject node, int count) {
        // 如果连续字符数量小于3，则不替换
        if (count < 3) {
            return node.getCharacter();
        }

        // 获取字母表中该字符的前一个字母
        char currentChar = node.getCharacter();
        if (currentChar > 'a') {
            return (char) (currentChar - 1); // 返回字母表中的前一个字母
        } else {
            return 'z'; // a的前一个是z
        }
    }

    @Override
    public boolean canReplace(char ch) {
        return ch >= 'a' && ch <= 'z';
    }
}
